package itext.test.example;

import java.io.FileOutputStream;


import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class Handler {

	public static void main(String[] args) throws Exception {
		
		float[] widths = { 144, 113, 191 };
		FileOutputStream fos = new FileOutputStream("d:/1.pdf");

		Document document = new Document();
		PdfWriter writer = PdfWriter.getInstance(document, fos);
		writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
		document.setPageSize(PageSize.A4);// 设置A4
		document.open();

		PdfPTable table = new PdfPTable(widths);
		table.setTotalWidth(458);
		table.setHorizontalAlignment(Element.ALIGN_LEFT);

		Object[][] datas = { { "区域", "总销售额(万元)", "总利润(万元)简单的表格" }, { "江苏省", 9045, 2256 }, { "广东省", 3000, 690 } };
		for (int i = 0; i < datas.length; i++) {
			for (int j = 0; j < datas[i].length; j++) {
				PdfPCell pdfCell = new PdfPCell(); // 表格的单元格
				Paragraph paragraph = new Paragraph(String.valueOf(datas[i][j]), getPdfChineseFont());
				pdfCell.setPhrase(paragraph);
				table.addCell(pdfCell);
			}
		}

		document.add(table); // pdf文档中加入table
		document.close();
	}

	public static Font getPdfChineseFont() throws Exception {
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
		return fontChinese;
	}
	//
	// private static String CHINESE_FONT = "SIMSUNB.TTF";//
	// 仿宋字体，在C:/Windows/fonts里找的font文件放到src目录下，参见
	// // 博客1
	// public static final Rectangle PAGE_SIZE = PageSize.A4;
	// public static final float MARGIN_LEFT = 50;
	// public static final float MARGIN_RIGHT = 50;
	// public static final float MARGIN_TOP = 50;
	// public static final float MARGIN_BOTTOM = 50;
	// public static final float SPACING = 20;
	//
	// public static void createPDF(String fileName, String content) throws
	// DocumentException, IOException {
	// File file = new File(fileName);// 生成的文件
	// FileOutputStream fout = new FileOutputStream(file);// 输出流
	// Document document = new Document(PAGE_SIZE, MARGIN_LEFT, MARGIN_RIGHT,
	// MARGIN_TOP, MARGIN_BOTTOM);// 页面大小以及布局
	// PdfWriter.getInstance(document, fout);// 将文档添加的输出流
	// document.open();// 打开文档准备写入
	// BaseFont baseFont = BaseFont.createFont(CHINESE_FONT,
	// BaseFont.IDENTITY_H, BaseFont.EMBEDDED);// 创建一个支持中文的字体
	// Font chinese = new Font(baseFont, 20.0f, Font.BOLDITALIC,
	// BaseColor.RED);// 博客1里的BaseColor在我下载的jar包中没有就用java.awt.color代替了
	// document.add(new Paragraph(content, chinese));// 写入内容
	// document.close();// 关闭文档
	// }
	//
	// public static void main(String[] args) throws DocumentException,
	// IOException {
	// createPDF("d:/test1.pdf", "正在使用iText创建一个包含中文的pdf文档！");
	// }
}
